package com.taxigang.client.uibinder_mobile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasAllFocusHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;

import com.google.gwt.jquery.mobile.ui.Button;
import com.google.gwt.jquery.mobile.ui.TextBox;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.micro.Hideable;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.ButtonType;
import com.taxigang.client.ui.map.MapView;
import com.taxigang.client.ui.map.MapView.ButtonMode;
import com.taxigang.client.ui.map.MapView.TextBoxType;
import com.taxigang.client.ui.map.SearchView;
import com.taxigang.client.uibinder.TaxiGangPanel;
import com.taxigang.shared.TaxiGangInfo;

public class SearchMobile extends TaxiGangPanel implements SearchView{

	private static SearchMobileUiBinder uiBinder = GWT
			.create(SearchMobileUiBinder.class);

	interface SearchMobileUiBinder extends UiBinder<Widget, SearchMobile> {
	}

	private Map<ButtonType,Button>searchCreateButtonMap = new HashMap<ButtonType,Button>();

	MultiWordSuggestOracle citySuggestOracle = new MultiWordSuggestOracle();  
	MultiWordSuggestOracle addressSuggestOracle = new MultiWordSuggestOracle();  

	@UiField
	TextBox startTextBox;

	@UiField
	TextBox destinationTextBox;

	@UiField
	Button searchButton;

	@UiField
	Button createButton;
	
//	@UiField
//	Button okButtonStartCity;
//	@UiField
//	Button okButtonStartAddress;
//	@UiField
//	Button okButtonDestinationCity;
//	@UiField
//	Button okButtonDestinationAddress;
//	@UiField
//	Button okButtonDepartureTime;

	@UiField
	Label startLabel;
	@UiField
	Label destinationLabel;
	
	@UiField
	Label headerLabel;
	
	@UiField
	Label departureLabel;
	
	@UiField
	ListBox listBoxDestinationTime;
	
	@UiField
	Panel searchButtonPanel;
	
//		
//	@UiField
//	Panel timePanel;
		
	public SearchMobile() {
		initWidget(uiBinder.createAndBindUi(this));
		
		headerLabel.setText(TaxiGangCreator.taxiGangConstants.search());
		//layoutPanel.setBorderWidth(10);
		searchButton.setText(TaxiGangCreator.taxiGangConstants.search());
		createButton.setText(TaxiGangCreator.taxiGangConstants.create());
		
		

//		okButtonDepartureTime.setText("OK");
		startLabel.setText(TaxiGangCreator.taxiGangConstants.enterStart());
		destinationLabel.setText(TaxiGangCreator.taxiGangConstants.enterDestination());
		departureLabel.setText(TaxiGangCreator.taxiGangConstants.departureTime());
		
		searchCreateButtonMap.put(ButtonType.Create, createButton);
		searchCreateButtonMap.put(ButtonType.Search, searchButton);

		Date calendar = new Date();
		//calendar.setMinutes(calendar.getMinutes()+30);
		int nowHour = calendar.getHours();
		int nowQuarter = calendar.getMinutes()/15;
		nowQuarter++;
		if (nowQuarter >=4){
			nowQuarter = 0;
			nowHour++;
		}
		boolean begin = true;
		for (int h=nowHour; h < 24+nowHour; h++){
			for (int q=0; q < 4; q++){
				if (begin){
					begin=false;
					q=nowQuarter;
				}
				boolean today = (h<24);
				int displayHour = h;
				if (!today){
					displayHour -=24;
				}
				int mins =  q*15;
				int value = displayHour*60 + mins;
				String display = displayHour+":"+mins;
				if (mins==0){
					display +="0";
				}
				if (!today){
					display += "  tomorrow";
				}
				listBoxDestinationTime.addItem(display,""+value);
			}
		}
		
		setMode(SearchDisplayMode.START);
		
//		final Map<SuggestBox,Button>suggestBoxMap = new HashMap<SuggestBox, Button>();
//		suggestBoxMap.put(startCitySuggestion, okButtonStartCity);
//		suggestBoxMap.put(startAddressSuggestion, okButtonStartAddress);
//		suggestBoxMap.put(destinationCitySuggestion, okButtonDestinationCity);
//		suggestBoxMap.put(destinationAddressSuggestion, okButtonDestinationAddress);
//		for (final SuggestBox suggestBox : suggestBoxMap.keySet()){
//			suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
//			
//			@Override
//			public void onFocus(FocusEvent event) {
//				for (Button b: okButtonMap.values()){
//					b.setVisible(false);
//				}
//				suggestBoxMap.get(suggestBox).setVisible(true);
//				
//			}
//		});
//		}
		
//		startCityTextBox.addFocusHandler(new FocusHandler() {
//			
//			@Override
//			public void onFocus(FocusEvent event) {
//				DomEvent.
//				if (event.getType().equals(obj))
//				Window.alert("focus");
//				
//			}
//		});
		startTextBox.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				Window.alert(":"+new Integer(event.getCharCode()));
				
			}
		});
	}

	@Override
	public Hideable getHideable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HasText getTextBoxText(MapView.TextBoxType tbt){
		HasText tb = null;
		switch (tbt){
		case START: 
			tb = startTextBox;  
			break;
		case DESTINATION: tb = destinationTextBox;  break;
		
		}
		return tb;
	}

	@Override
	public SuggestBox getSuggestBox(TextBoxType tbt) {
		if (true) throw new RuntimeException("TODO");
		return null;
	}

	@Override
	public HasKeyPressHandlers getTextBox(TextBoxType tbt) {
		HasKeyPressHandlers tb = null;
		switch (tbt){
		case START: tb = startTextBox;  break;
		case DESTINATION: tb = destinationTextBox;  break;

		}
		return tb;
	}

	@Override
	public  HasSelectionHandlers<Suggestion> getSuggestion(MapView.TextBoxType tbt){

		if (true)
			throw new RuntimeException();

		HasSelectionHandlers<Suggestion> tb = null;
//		switch (tbt){
//		case START_CITY: tb = startCityTextBox;  break;
//		case START_ADDRESS: tb = startAddressSuggestion;  break;
//		case DESTINATION_CITY: tb = destinationCitySuggestion;  break;
//		case DESTINATION_ADDRESS: tb = destinationAddressSuggestion;  break;
//
//		}
		return tb;
	}

	@Override
	public HasClickHandlers getButton(ButtonType buttonType) {
		return searchCreateButtonMap.get(buttonType);
	}

	@Override
	public HasAllFocusHandlers getFocus(TextBoxType tbt) {
		HasAllFocusHandlers tb = null;
		switch (tbt){
		case START: tb = startTextBox;  break;
		case DESTINATION: tb = destinationTextBox;  break;

		}
		return tb;
	}



	@Override
	public HasClickHandlers getOKTimeButton() {
		if (true)
			throw new RuntimeException();
		return null;
		//return okButtonDepartureTime;
	}

	@Override
	public void updateDepartureTime() {
		int index = listBoxDestinationTime.getSelectedIndex();
		int hour = index/24;
		int min = (index%4)*15;
		//Window.alert("updateDepartureTime:"+hour+":"+min);
		TaxiGangInfo info = Session.getInstance().getSearchTaxiGangInfo();
		
//		Time t = timeField.getValue();
//		info.setDepartureHour(t.getHour());
//		info.setDepartureMinute(t.getMinutes());
//
//		int hour = timeField.getValue().getHour();
//		int min = timeField.getValue().getMinutes();
//		Log.info("search time:"+hour+":"+min);
//
		info.setDepartureHour(hour);
		info.setDepartureMinute(min);
		info.setCreationTime(new Date());
		
	}

	@Override
	public void setMode(SearchDisplayMode mode) {
//		for (final TextBoxType textBoxType : TextBoxType.values()){
//			okButtonMap.get(textBoxType).setVisible(false);
//		}

		switch (mode){
		case START:
			//startAddressSuggestion.setFocus(true);
			searchButtonPanel.setVisible(false);
			break;
		case DESTINATION:
			destinationLabel.setVisible(true);
			destinationTextBox.setVisible(true);
			//destinationCityTextBox.setValue(startCityTextBox.getValue());
			destinationTextBox.setFocus(true);
			break;
		case TIME:
			departureLabel.setVisible(true);
			listBoxDestinationTime.setVisible(true);
//			okButtonDepartureTime.setVisible(true);
			break;
		case SEARCH_CREATE:
			searchButtonPanel.setVisible(true);
//			okButtonDepartureTime.setVisible(false);
			//						searchButton.setVisible(true);
			//						searchButton.setFocus(true);
			break;



		}
		
	}


	@Override
	public void setDestination(String destination) {
		destinationTextBox.setText(destination);
		
	}

	@Override
	public void setTextBoxText(TextBoxType tbt, String text) {
		Window.alert("TODO:setTextBoxText");
		
	}

	@Override
	public void setButtonMode(ButtonMode mode) {
		Window.alert("TODO:setButtonMode");
		
	}
	
	public String getSelectedTime() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setStart(String city) {
		// TODO Auto-generated method stub
		
	}


}
